#include "type.h"
#include "mem/mem_switch.h"
#include <string.h>
#include <iostream>

NNE50v2MemSwitch::NNE50v2MemSwitch(NNE50v2MemSwitch::RamMapType rams) : RamMap(rams){
}

void NNE50v2MemSwitch::write_mem(u64 dst, void* src, u64 length){
    NNE50v2MemSwitch::RamMapType::const_iterator it;
    it = RamMap.upper_bound(dst);
    if(it == RamMap.begin())
    {
        CHECK_MSG(false,"wrong dst address!");
        return;
    }
    --it;
    if(it->second.addr_length + it->first <= dst)CHECK_MSG(false, "wrong dst address!");
    u64 real_mem_addr = dst - it->first + it->second.real_start_addr;
    it->second.memory->write_mem(real_mem_addr, src, length);
    return;
}

void NNE50v2MemSwitch::read_mem(void*dst, u64 src, u64 length){
    NNE50v2MemSwitch::RamMapType::const_iterator it;
    it = RamMap.upper_bound(src);
    if(it == RamMap.begin())
    {
        CHECK_MSG(false,"wrong src address!");
        return;
    }
    --it;
    if(it->second.addr_length + it->first <= src)CHECK_MSG(false, "wrong src address!");
    u64 real_mem_addr = src - it->first + it->second.real_start_addr;
    it->second.memory->read_mem(dst, real_mem_addr, length);
    return;
}
